package com.project.livetv.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.project.livetv.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


public class SMSActivity extends BaseActivity {
    private ImageButton back_imagebutton;
    private EditText phone_editText,mcode_editText;
    private Button login_btn;
    private String phone;
    private int time=60;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == -9) {
                login_btn.setText("(" + time + " s )");
            } else if (msg.what == -8) {
                login_btn.setText("获取验证码");
                login_btn.setClickable(true);
                time = 60;
            } else {
                int event = msg.arg1;
                int result = msg.arg2;
                Object data = msg.obj;
                Log.e("event", "event=" + event);
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // 短信注册成功后，返回MainActivity,然后提示
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
                        Toast.makeText(getApplicationContext(), "验证码成功",
                                Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(SMSActivity.this, LoginActivity.class);
                        intent.putExtra("phone",phone);
                        startActivity(intent);


                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        Toast.makeText(getApplicationContext(), "正在获取验证码",
                                Toast.LENGTH_SHORT).show();
                    } else {
                            Toast.makeText(SMSActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                        ((Throwable) data).printStackTrace();
                    }
                }
            }
        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        initSMS();
    }

    @Override
    public void initView() {
     back_imagebutton= (ImageButton) findViewById(R.id.back_imagebtn);
        phone_editText= (EditText) findViewById(R.id.phone_editText);
        mcode_editText= (EditText) findViewById(R.id.code_editText);
        login_btn= (Button) findViewById(R.id.login_btn);

    }

    @Override
    public void initData() {

    }

    @Override
    public void setData() {

    }

    @Override
    public void setListener() {

    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.back_imagebtn:
                finish();
                break;
            case R.id.login_btn:
                phone = phone_editText.getText().toString();
                if (isInputCorrect(phone)==true) {
                    SMSSDK.getVerificationCode("86", phone);
                    login_btn.setClickable(false);
                    login_btn.setText( time+" s");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (; time > 0; time--) {
                                handler.sendEmptyMessage(-9);
                                if (time < 0) {
                                    break;
                                }
                                try {
                                    Thread.sleep(1 * 1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            handler.sendEmptyMessage(-8);
                        }
                    }).start();
                }
                break;
            case R.id.submit_complete:
                phone = phone_editText.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    //设置吐司提示
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    return ;
                }
                if (TextUtils.isEmpty(mcode_editText.getText())) {
                    //设置吐司提示
                    Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isInputCorrect(phone)==false) {
                    return;
                } else {
                    SMSSDK.submitVerificationCode("86", phone, mcode_editText.getText().toString());
                }
                break;
        }
    }
    private void initSMS() {
        SMSSDK.initSDK(this, "12769b182978d", "869006761670511c3142c4db803aa01e");
        EventHandler eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                handler.sendMessage(msg);
            }
        };
        SMSSDK.registerEventHandler(eh); //注册短信回调
    }


    //判断手机号输入是否正确的正则表达式
    public static boolean regexTel(String tel) {
        Pattern pattern = Pattern.compile("^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$");
        Matcher matcher = pattern.matcher(tel);
        return matcher.matches();
    }


    //判断输入是否合理
    private boolean isInputCorrect(String phone) {
        if (TextUtils.isEmpty(phone)) {
            //设置吐司提示
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        //手机注册
        if (regexTel(phone) == false) {
            Toast.makeText(SMSActivity.this, "手机号输入不正确", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }


}
